#!/usr/bin/env bash

# Git Hook for JIRA_TASK_ID
# Adds to the top of your commit message `JIRA_TASK_ID`, based on the prefix of the current branch `feature/AWG-562-add-linter`
# Example: `Add SwiftLint -> `AWG-562 Add SwiftLint

if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP='master develop test'
fi

CURRENT_BRANCH=$(git branch --show-current)
for BRANCH in $BRANCHES_TO_SKIP; do
  if [ "$BRANCH" = "$CURRENT_BRANCH" ]; then
    echo "Info, skipping Jira ID check since current branch is included in the 'BRANCHES_TO_SKIP' variable"
    exit 0
  fi
done

COMMIT_FILE=$1
COMMIT_MSG=$(cat "$1")
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID_REGEX="[A-Z0-9]{1,10}-?[A-Z0-9]+"
JIRA_ID_IN_CURRENT_BRANCH_NAME=$(echo "$CURRENT_BRANCH" | grep -Eo "$JIRA_ID_REGEX")
JIRA_ID_IN_COMMIT_MESSAGE=$(echo "$COMMIT_MSG" | grep -Eo "$JIRA_ID_REGEX")

if [ -n "$JIRA_ID_IN_COMMIT_MESSAGE" ]; then
  if [ "$JIRA_ID_IN_COMMIT_MESSAGE" != "$JIRA_ID_IN_CURRENT_BRANCH_NAME" ]; then
    echo "Error, your commit message JIRA_TASK_ID='$JIRA_ID_IN_COMMIT_MESSAGE' is not equal to current branch JIRA_TASK_ID='$JIRA_ID_IN_CURRENT_BRANCH_NAME'"
    exit 1
  fi
elif [ -n "$JIRA_ID_IN_CURRENT_BRANCH_NAME" ]; then
  echo "$JIRA_ID_IN_CURRENT_BRANCH_NAME $COMMIT_MSG" > "$COMMIT_FILE"
  echo "JIRA ID '$JIRA_ID_IN_CURRENT_BRANCH_NAME', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
fi
